<template>
  <div class="update-gender">
    <van-picker
      show-toolbar
      title="性别"
      :columns="columns"
      :default-index='defaultIndex'
      @cancel="$emit('close')"
      @confirm="onConfirm"
      @change="handleGenderChange"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      defaultIndex: this.value
    }
  },
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true
      })
      await updateUserProfile({
        gender: this.defaultIndex
      })
      this.$toast.success('保存成功')
      this.$emit('close')
      // 修改成功更新视图的方式
      //  1. 重新获取数据
      //  2. 单独前端更新字段(推荐)
      this.$emit('input', this.defaultIndex)// 方式1 可以使用v-model
    },
    handleGenderChange (picker, value, index) {
      this.defaultIndex = index
    }
  }
}

</script>
<style lang='less' scoped>
</style>
